package com.class5.springboot.frame.repo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Sql语句的封装
 * @author Administrator
 *
 */
public class Sql {
    private List<String> tokens = new ArrayList<>();
    private List<Object> params = new ArrayList<Object>();
    public Sql(String sql, Object[] params) {
        this.addSub(sql, params);
    }
    public Sql() {

    }
    public static Sql get(){
        return new Sql();
    }
    public static Sql get(String sql, Object... params){
        return get().addSql(sql, params);
    }
    /**
     * 根据实体类的属性生成对应的查询语句
     * @return
     */
    public static Sql getSelect(Class<?> entityClass, String columnPrfix){
        String str = "#{opt:'select',cla:'"+entityClass.getName()+"',prefix:'"+(columnPrfix==null||columnPrfix.isEmpty()?"":columnPrfix)+"'}";
        return get().addSub(str);
    }
    /**
     * 根据实体类的属性生成对应的查询语句
     * @return
     */
    public static Sql getSelect(Class<?> entityClass){
        return getSelect(entityClass, null);
    }

    /**
     * 添加条件
     * @param condi
     * @return
     */
    public Condition add(){
        return new Condition("", this);
    }

    /**
     * 添加AND条件
     * @param condi
     * @return
     */
    public Condition and(){
        return new Condition("AND", this);
    }
    /**
     * 添加OR条件
     * @param condi
     * @return
     */
    public Condition or(){
        return new Condition("OR", this);
    }
    /**
     * 添加WHERE条件
     * @param condi
     * @return
     */
    public Condition where(){
        return new Condition("WHERE", this);
    }
    /**
     * 添加and条件
     * @param params
     * @return
     */
    public Sql and(String sql, Object... params){
        if(sql != null && !sql.isEmpty()){
            return addSub("AND (" + sql + ")", params);
        }
        return this;
    }
    /**
     * 添加OR条件
     * @param params
     * @return
     */
    public Sql or(String sql, Object... params){
        if(sql != null && !sql.isEmpty()){
            return addSub("OR (" + sql + ")", params);
        }
        return this;
    }
    /**
     * 添加WHERE条件
     * @param params
     * @return
     */
    public Sql where(String sql, Object... params){
        if(sql != null && !sql.isEmpty()){
            return addSub("WHERE (" + sql + ")", params);
        }
        return this;
    }
    /**
     * 添加OrderBy
     * @param column
     * @param flag
     * @return
     */
    public Sql orderBy(String column, String flag){
        return addSub("ORDER BY " + column + " "+ (flag==null?" ASC":flag));
    }

    /**
     * 添加GroupBy
     * @param sql
     * @param params
     * @return
     */
    public Sql addGroupBy(String columns){
        return addSub("GROUP BY " + columns);
    }
    /**
     * 添加子句
     * @param sql
     * @param params
     * @return
     */
    public Sql addSql(String sql, Object... params){
        addSub(sql, params);
        return this;
    }
    /**
     * 添加子元素
     * @param sql
     * @param params
     * @return
     */
    private Sql addSub(String sql, Object... params){
        if(sql == null || sql.isEmpty())
            return this;
        this.tokens.add(sql + " ");
        if(params != null)
            this.params.addAll(Arrays.asList(params));
        return this;
    }
    /**
     * 添加条件
     * @param condition
     * @return
     */
    public Sql addCondition(Condition condition){
        this.addSql(condition.getSql(), condition.getParams());
        return this;
    }
    @Override
    public String toString() {
        return "Sql:" + getSql() + ",params:" + params;
    }
    /**
     * 获取语句
     * @return
     */
    public String getSql(){
        StringBuffer sb = new StringBuffer();
        for(String str : tokens){
            sb.append(str);
        }
        return sb.toString().trim();
    }
    /**
     * 获取参数
     * @return
     */
    public Object[] getParams(){
        return this.params.toArray();
    }
    /**
     * 获取参数列表
     * @return
     */
    public List<Object> getParamsList() {
        return params;
    }



	/*public static void main(String[] args) {
		Sql sql = Sql.get("select id from user where compid = ?", 12L)
				.and()
				.eq("name", "jerry")
				.eq("id", null)
				.gte("age", 20)
				.gt("age", 10)
				.in("sex", "f","m")
				.lte("amount", 200f)
				.lt("amount", null)
				.like("foo", "foo1")
				.llike("name", null)
				.rlike("foo", "foo2")
				.eqMust("birth", null)
				.isNotNull("bar")
				.exists("select id from dept where userid = id and name = ?", "foo", "KK")
				.add("col=? and col2 >?", 1, null)
				.parent()
				.or("sex = ?", "F")
				.orderBy("id", "desc");
		System.out.println(sql.getSql());
		System.out.println(sql.getParamsList());
	}*/


}
